import WeCropper from '../../we-cropper/we-cropper.js'
const app = getApp();

const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight - 50

Page({
  data:{
    selectCover: true,
    cropperOpt: {
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 218) / 2,
        y: (height - 170) / 2,
        width: 218,
        height: 170
      }
    }
  },



  onLoad:function(option){
    var that = this;
    const { cropperOpt } = this.data;
    
    new WeCropper(cropperOpt)
      .on('ready', (ctx) => {
        console.log(`wecropper is ready for work!`)
      })
      .on('beforeImageLoad', (ctx) => {
        console.log(`before picture loaded, i can do something`)
        console.log(`current canvas context:`, ctx)
      })
      .on('imageLoad', (ctx) => {
        console.log(`picture loaded`)
        console.log(`current canvas context:`, ctx)
        wx.hideToast()
      })
      .on('beforeDraw', (ctx, instance) => {
        console.log(`before canvas draw,i can do something`)
        console.log(`current canvas context:`, ctx)
      })
      .updateCanvas();

       that.wecropper.pushOrign(option.url)
  },

  //截图方法

  touchStart(e) {
    this.wecropper.touchStart(e)
  },
  touchMove(e) {
    this.wecropper.touchMove(e)
  },
  touchEnd(e) {
    this.wecropper.touchEnd(e)
  },
  getCropperImage() {
    var that = this;
    that.setData({
      selectCover: false
    });
    wx.showLoading({
      title: '封面上传中...',
    })
    this.wecropper.getCropperImage((src) => {
      console.log('裁图完成!')
      if (src) {
        wx.uploadFile({
          url: app.globalData.phpUrl + 'Publish/api',
          filePath: src,
          name: 'img',
          header: {
            "Content-Type": "multipart/form-data",
          },
          formData: {
            'api_name': 'upload',
            'type': 1,
          },
          success: function (ress) {
            console.log(ress)
            wx.hideLoading();
            if (JSON.parse(ress.data).code == -1) {
              wx.showToast({
                title: JSON.parse(ress.data).msg + '！',
                icon: 'none',
                duration: 2000,
              });
            };
            if (JSON.parse(ress.data).code == 1) {
              getCurrentPages()[getCurrentPages().length - 2].setData({
                ['subject.banner']: JSON.parse(ress.data).data,
                banner: JSON.parse(ress.data).data,
              })
            };
            wx.navigateBack({})
          },
          fail: function (e) {
            wx.hideLoading();
            wx.showToast({
              title: '上传失败!',
              icon: 'none',
              duration: 2000,
            });
            wx.navigateBack({})
          }
        });

      } else {
        wx.hideLoading();
        wx.showToast({
          title: '上传失败!',
          icon: 'none',
          duration: 2000,
        });
        wx.navigateBack({})
        console.log('获取图片地址失败，请稍后重试')
      }
    })
  },
  cancelCover() {
    wx.navigateBack({})
  }
  
})